﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AresCustomYacht
{
    public partial class P_WorkLocation : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        private int _mode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            var tradeId = int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            Utility.DisplayPageMessage(this, lblMessage);

            _mode = Utility.GetPageMode(this);
            if (_mode == 1) //Data mode
            {
                btnAdd.Visible = true;
                btnNext.Visible = true;
                

            }
            else
            {
                btnAdd.Visible = false;
                btnNext.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                var locations = _bl.GetClassLocations(tradeId);
                locations.ForEach(p=> p.Name = p.Code + "  " + p.Name);
                lstLocation.DataSource = locations;
                lstLocation.DataTextField = "Name";
                lstLocation.DataValueField = "Id";
                lstLocation.DataBind();
                lstLocation.SelectedIndex = 0;
            }
            

        }

        protected void lstLocation_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var message = Utility.GetQueryStringItem(this, "Message");

            int boatId = 0;
            if (_mode != 1) //Data Mode
                boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));
            var linkAddress =
                "Mode="+ _mode +"&BoatId=" + boatId + "&ParentId=" + lstLocation.SelectedValue + "&Message=" + message + ">" + lstLocation.SelectedItem.Text;
            if (dffBookmark.Checked)
            {
                linkAddress += "&Bookmark=P-Location.aspx&BookmarkId=" +
                               int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            }
            else if (!string.IsNullOrEmpty(Utility.GetQueryStringItem(this, "Bookmark")))
            {
                linkAddress += "&Bookmark=" + Utility.GetQueryStringItem(this, "Bookmark") + "&BookmarkId=" +
                               Utility.GetQueryStringItem(this, "BookmarkId");
            }
            Response.Redirect("P-Task.aspx?k=" + Utility.Encrypt(linkAddress));
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("P-BatchAdd.aspx?k=" + Utility.Encrypt("Mode=0&What=Task&ParentId=" + lstLocation.SelectedValue + "&Message=Add related Tasks to[" + lstLocation.SelectedItem.Text + "]"));
        }
    }
}